home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / edcron < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.7 KB  |  111 lines

  1. #!/bin/ksh
  2. # @(#) edcron.ksh 2.0 96/01/19
  3. # 93/02/13 John H. DuBois III (john@armory.com)
  4. # 93/04/30 Allow user names on command line.
  5. # 93/07/18 Added options & other functionality.
  6. # 93/07/26 Fixed removal of tmpfile
  7. # 93/10/30 Added -c option
  8. # 94/04/17 Print time of last modification
  9. # 96/01/19 Moved default tmpdir to home dir for safety
  10.  
  11. alias istrue="test 0 -ne"
  12. alias isfalse="test 0 -eq"
  13.  
  14. # Usage: doed <username>
  15. # The test for whether a file was modified is to save its contents in a
  16. # shell variable & check after editing for whether the contents are the same.
  17. function doed {
  18.     typeset file ans tmp="$TMPDIR/#$1.$$" user=$1 tfile
  19.  
  20.     if crontab -l -u "$user" > $tmp || istrue create; then
  21.     file="$(<$tmp)"
  22.     $VISUAL $tmp
  23.     tfile=/usr/spool/cron/crontabs/$user
  24.     # Print this *after* editing so that it won't be immediately
  25.     # cleared from the screen by the editor startup (unless more than
  26.     # one crontab file is being edited)
  27.     if [ -f $tfile ]; then
  28.         set -- $(l $tfile)
  29.         print -u2 "$user's crontab file was last modified $6 $7 $8"
  30.     fi
  31.     if [ "$file" = "$(<$tmp)" ]; then
  32.         print -u2 "Not modified; skipping $user..."
  33.     else
  34.         if istrue interactive; then
  35.         echo -n "Submit crontab file for $user? "
  36.         read ans
  37.         if [[ "$ans" != [yY]* ]]; then
  38.             print "crontab for $user not submitted."
  39.             rm -f $tmp
  40.             return 0
  41.         fi
  42.         fi
  43.         crontab -u "$user" $tmp &&
  44.         print "crontab for $user submitted."
  45.     fi
  46.     else
  47.     print -u2 "Skipping $user..."
  48.     fi
  49.     rm -f $tmp
  50. }
  51.  
  52. DefEd=vi
  53. typeset -i interactive=0 create=0
  54. : ${VISUAL:=$EDITOR}
  55. : ${VISUAL:=$DefEd}
  56. : ${TMPDIR:=$TMP}
  57. : ${TMPDIR:=$HOME}
  58.  
  59. name=${0##*/}
  60. Usage="Usage: $name [-chi] [user ...]"
  61.  
  62. while getopts :chi opt; do
  63.     case $opt in
  64.     h)
  65.     echo \
  66. "$name: edit crontab files.
  67. $Usage
  68. Each user's crontab file is presented for editing.
  69. When the editor is exited, the file is submitted to cron and the
  70. next file, if any, is presented.  If the editor is exited without
  71. the file being modified, it is not submitted.  If the environment
  72. variable VISUAL or EDITOR is set, its value is used for the editor.
  73. If not, $DefEd is used.
  74. Options:
  75. -c: Create crontab file if it does not exist.
  76. -h: Print this help info.
  77. -i: Interactive operation.  Ask before submitting new crontab file."
  78.     exit 0
  79.     ;;
  80.     c)
  81.     create=1
  82.     ;;
  83.     i)
  84.     interactive=1
  85.     ;;
  86.     +?)
  87.     print -u2 "$name: options should not be preceded by a '+'."
  88.     exit 1
  89.     ;;
  90.     :) 
  91.     print -r -u2 -- \
  92.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  93.     exit 1
  94.     ;;
  95.     ?) 
  96.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  97.     exit 1
  98.     ;;
  99.     esac
  100. done
  101.  
  102. # remove args that were options
  103. let OPTIND=OPTIND-1
  104. shift $OPTIND
  105.  
  106. [ $# -lt 1 ] && set -- $USER
  107.  
  108. for user; do
  109.     doed "$user"
  110. done
  111.